package com.manage.DB;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.manage.handler.BugHandler;

public class BugController {
	// 数据库名称
	private static String DB_NAME = "bugmanage.db";
	// 数据库版本
	private static int DB_VERSION = 2;
	private SQLiteDatabase db;
	private SqliteBugHelper dbBugHelper;

	// private SqliteHelper dbHelper;

	public BugController(Context context) {
		dbBugHelper = new SqliteBugHelper(context, DB_NAME, null, DB_VERSION);
		db = dbBugHelper.getWritableDatabase();
	}

	public void Close() {
		db.close();
		dbBugHelper.close();
	}

	// 获取users表中的USER_ID、Access Token、Access Secret的记录
	public List<BugHandler> GetUserList(Boolean isSimple) {
		List<BugHandler> userList = new ArrayList<BugHandler>();
		Cursor cursor = db.query(SqliteBugHelper.TB_NAME, null, null, null, null, null, BugHandler.BUG_ID + " DESC");
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && (cursor.getString(1) != null)) {
			BugHandler bug = new BugHandler();
			bug.setBugId(cursor.getString(0));
			bug.setBugName(cursor.getString(1));
			bug.setBugDescribe(cursor.getString(2));
			bug.setBugDate(cursor.getString(3));
			bug.setBugBelongProject(cursor.getString(4));
			bug.setBugStatus(cursor.getString(5));
			userList.add(bug);
			cursor.moveToNext();
		}
		cursor.close();
		return userList;
	}

	public boolean isNullSinaTb() {
		Boolean flag = false;
		Cursor cursor = db.query(SqliteBugHelper.TB_NAME, null, null, null, null, null, BugHandler.BUG_ID + " DESC");
		flag = !cursor.moveToFirst();
		cursor.close();
		return flag;
	}

	// 判断users表中的是否包含某个USER_ID的记录
	public boolean HaveUserInfo(String userId) {
		Boolean b = false;
		Cursor cursor = db.query(SqliteBugHelper.TB_NAME, null, BugHandler.BUG_ID + "=" + userId, null, null, null, null);
		b = cursor.moveToFirst();
		// Log.e("HaveUserInfo", b.toString());
		cursor.close();
		return b;
	}

	// 更新users表的记录，根据USER_ID更新用户昵称和用户图标
	public int UpdateUserInfo(String userName, String userId) {
		ContentValues values = new ContentValues();
		values.put(BugHandler.BUG_NAME, userName);
		int id = db.update(SqliteBugHelper.TB_NAME, values, BugHandler.BUG_ID + "=" + userId, null);
		// Log.e("UpdateUserInfo2", id + "");
		return id;
	}

	// 更新users表的记录
	public int UpdateUserInfo(BugHandler bug) {
		ContentValues values = new ContentValues();
		values.put(BugHandler.BUG_DATE, bug.getBugDate());
		values.put(BugHandler.BUG_NAME, bug.getBugName());
		values.put(BugHandler.BUG_DESCRIBE, bug.getBugDescribe());
		values.put(BugHandler.BUG_BELONG_PROJECT, bug.getBugBelongProject());
		values.put(BugHandler.BUG_STATUS, bug.getBugStatus());
		int id = db.update(SqliteBugHelper.TB_NAME, values, BugHandler.BUG_ID + "=" + bug.getBugId(), null);
		// Log.e("UpdateUserInfo", id + "");
		return id;
	}

	// 添加users表的记录
	public Long SaveBugInfo(BugHandler bug, Boolean isSimple) {
		ContentValues values = new ContentValues();
		values.put(BugHandler.BUG_NAME, bug.getBugName());
		values.put(BugHandler.BUG_DATE, bug.getBugDate());
		values.put(BugHandler.BUG_DESCRIBE, bug.getBugDescribe());
		values.put(BugHandler.BUG_BELONG_PROJECT, bug.getBugBelongProject());
		values.put(BugHandler.BUG_STATUS, bug.getBugStatus());

		Long uid = db.insert(SqliteBugHelper.TB_NAME, BugHandler.BUG_ID, values);
		// Log.e("SaveUserInfo", uid + "");
		return uid;
	}

	// 删除users表的记录
	public int DelUserInfo(String userId) {
		int id = db.delete(SqliteBugHelper.TB_NAME, BugHandler.BUG_ID + "=" + userId, null);
		// Log.e("DelUserInfo", id + "");
		return id;
	}
}
